/*
 * @author java_deng
 * @date 2024/11/29 9:45
 * @description
 */
package org.kaka.controller;

import cn.hutool.json.JSONUtil;
import org.kaka.dto.UserDto;
import org.kaka.service.impl.UserService;
import org.kaka.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public Object login(UserDto userDto){
        String exception=checkParams(userDto);
        if(exception!=null){
            return fail(exception);
        }
        Long userId=userService.login(userDto);
        System.out.println(userId);
        if(userId==0){
            return fail("密码或账号不正确");
        }
        else {
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), JSONUtil.toJsonStr("登录成功"), null);
            return success(userId.toString(),token,0);
        }
    }

    private String checkParams(UserDto userDto) {
        String password = userDto.getPassword();
        String account = userDto.getAccount();
        if(password==null){
            return "密码不能为空";
        }
        if(account==null){
            return "账号不能为空";
        }
        return null;
    }
}
